﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;

public partial class Product_Select : PageBase
{
    private const string ValueControl_Id = "ValueControlId";
    private string valueControl;
    private decimal repID;
    private decimal customerID;


    ObjectTable ot;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[ValueControl_Id] != null)
        {
            valueControl = Request.QueryString[ValueControl_Id];
        }

        decimal.TryParse(Request["repID"],out repID);
        decimal.TryParse(Request["customerID"], out customerID);

        if (!IsPostBack)
        {
            GetManuType();
            GetCategoryType();
            GetBrandType();
            GetFormType();
            //this.InitForm();
        }
    }

    #region 绑定品牌
    public void GetBrandType()
    {
        ddlBrand.DataTextField = "BrandName";
        ddlBrand.DataValueField = "BrandCode";
        ot = BrandManager.GetBrands(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "active=true";
            ddlBrand.DataSource = ot.BillDataTable.DefaultView;
            ddlBrand.DataBind();
        }

        ddlBrand.Items.Insert(0, new ListItem(Resources.language.ddl_All, ""));
    }
    #endregion

    #region 绑定系列
    public void GetFormType()
    {
        ddlForm.DataTextField = "FormName";
        ddlForm.DataValueField = "FormCode";
        ot = FormManager.GetFORMS(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "Valid='1'";
            ddlForm.DataSource = ot.BillDataTable.DefaultView;
            ddlForm.DataBind();
        }
        ddlForm.Items.Insert(0, new ListItem(Resources.language.ddl_All, ""));
    }
    #endregion

    #region 绑定制造商
    public void GetManuType()
    {
        ddlManu.DataTextField = "ManuName";
        ddlManu.DataValueField = "ManuCode";
        ot = ManufacturersManager.GetManus(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=true";
            ddlManu.DataSource = ot.BillDataTable.DefaultView;
            ddlManu.DataBind();
        }
        ddlManu.Items.Insert(0, new ListItem(Resources.language.ddl_All, ""));
    }
    #endregion

    #region 绑定品类
    public void GetCategoryType()
    {
        ddlCategroy.DataTextField = "CategoryName";
        ddlCategroy.DataValueField = "CategoryCode";
        ot = CategoryManager.GetCategories(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=true";
            ddlCategroy.DataSource = ot.BillDataTable.DefaultView;
            ddlCategroy.DataBind();
        }
        ddlCategroy.Items.Insert(0, new ListItem(Resources.language.ddl_All, ""));
    }
    #endregion

    #region 制造商改变事件
    protected void ddlManu_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlBrand.DataTextField = "BrandName";
        ddlBrand.DataValueField = "BrandCode";
        DataSet ds = BrandManager.GetBrandsListDaynamic(ddlCategroy.SelectedValue, ddlManu.SelectedValue, MyStateManager.DomainID);
        ddlBrand.DataSource = ds;
        ddlBrand.DataBind();
        ddlBrand.Items.Insert(0, new ListItem(Resources.language.ddl_All, ""));
    }
    #endregion

    #region 品类改变事件
    protected void ddlCategroy_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlBrand.DataTextField = "BrandName";
        ddlBrand.DataValueField = "BrandCode";
        DataSet ds = BrandManager.GetBrandsListDaynamic(ddlCategroy.SelectedValue, ddlManu.SelectedValue, MyStateManager.DomainID);
        ddlBrand.DataSource = ds;
        ddlBrand.DataBind();
        ddlBrand.Items.Insert(0, new ListItem(Resources.language.ddl_All, ""));
    }
    #endregion

   

    private void BindGrid()
    {
        USERS user = new UserManager().GetUserByID(repID);

        gvProductList.DataSource = ProductManager.GetProductDynamic(customerID
                                                                 , txtProduct.Text.Trim().Replace("'", "''")
                                                                 ,ddlCategroy.SelectedValue
                                                                 ,ddlBrand.SelectedValue
                                                                 , ddlManu.SelectedValue
                                                                 , ddlForm.SelectedValue
                                                                 , MyStateManager.DomainID
                                                                 , "1"
                                                                ,"1"
                                                                 ).Tables[0];
        gvProductList.DataBind();

    }


    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
    }
    

    protected void gvProductList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvProductList.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        CheckBox cbxSelected;
        string productList = string.Empty;

        foreach (GridViewRow row in gvProductList.Rows)
        {
            cbxSelected = (CheckBox)row.FindControl("cbxSelect");

            if (cbxSelected.Checked)
            {
                productList += gvProductList.DataKeys[row.RowIndex].Value.ToString() + "∮";
            }
        }

        productList=productList.TrimEnd('∮');

        if (productList == string.Empty)
        {
            ShowMessage("请选择产品!");
            return;
        }


        //生成js脚本并传回父窗体
        StringBuilder str = new StringBuilder();
        str.AppendLine("<script language='javascript'>");

        //回传数值
        if (!string.IsNullOrEmpty(valueControl))
            str.AppendLine("    window.opener.document.getElementById('" + valueControl + "').value=\"" + productList + "\";");

        str.AppendLine("    window.opener.document.aspnetForm.submit();");
        str.AppendLine("    window.close();");
        str.AppendLine("</script>");

        Response.Write(str.ToString());



    }
}
